﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
namespace WCF_ListenMusic.Services.Module_Friend.User
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FriendService" in code, svc and config file together.
    [ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete=true)]
    public class FriendService : IFriendService
    {
        Connect_DBDataContext context = new Connect_DBDataContext();

        #region get Friends ByUser

        public List<FriendUserAB> getFriendsByUser(string username)
        {

            try
            {
                if (username == null)
                {
                    throw new FaultException("Username Trống");
                }
                else
                {
                    var q = from p in context.FriendUserABs
                            where (p.UserA.Equals(username) || p.UserB.Equals(username)) && p._Status == true
                            select p;
                    return q.ToList();

                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region IFriendService Members

        [OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=true)]
        public bool AddFriend(Friend friend)
        {
            try
            {
                context.Friends.InsertOnSubmit(friend);
              
                context.SubmitChanges();
                return true;
            }
            catch (FaultException ex)
            {
                return false;
                throw new FaultException(ex.Message);
            }
        }

        #endregion
    }

        
}
